/**
 * Wuhan University Library for Cryptography (WULC) is an ongoing and portable 
 * cryptographic toolkit developed by Cryptography and Blockchain Technology
 * Laboratory · Wuhan University. 
 * This file is part of WULC. 
 * @copyright Copyright (c) 2018-2023 WULC authors
 * 
 * @file SmartGrid.java
 * @brief to descirbe the ui
 * 
 * @author Qiao Hongyi (974446987@qq.com)
 * @version 0.1.1
 * @date 2023-01-16
 * 
 * @ingroup {group}
 */

/*
 * Created by JFormDesigner on Wed Apr 14 17:29:45 CST 2021
 */

package swing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.builder.HCB;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import pemks.entity.search_param;
import pemks.pemks_function;
import com.formdev.flatlaf.FlatDarculaLaf;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import org.apache.http.Header;
import org.apache.http.client.HttpClient;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 23
 */
public class SmartGrid {
    public static pemks_function pf = new pemks_function();

    public static String secret_kf;
    public static String Login_user;
    public static DefaultTableModel model;

    public SmartGrid() {
        initComponents();
    }

    private void choose_buttonActionPerformed(ActionEvent e) {
        JFileChooser jf = new JFileChooser();
        jf.showOpenDialog(User);//显示打开的文件对话框
        File f =  jf.getSelectedFile();//使用文件类获取选择器选择的文件
        String aimfile = f.getAbsolutePath();//返回路径名
        choose_textField.setText(aimfile);
    }

    private void update_buttonActionPerformed(ActionEvent e) throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException, HttpProcessException {
        String plainfile = choose_textField.getText();
        if(plainfile.equals("")){
            JOptionPane.showMessageDialog(null, "请选择文件！");
        }else{
            String tousername = touser_textField.getText();
            if(tousername.equals("")){
                JOptionPane.showMessageDialog(null, "请填写文件分享对象！");
            }else{
                String url = "http://127.0.0.1:8181/smartgrid/getuser_pk";
                Header[] headers 	= HttpHeader.custom()
                        .userAgent("javacl")
                        .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                        .other("customer", "自定义")
                        .build();
                HCB hcb= HCB.custom();
                HttpClient client = hcb.build();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("username", tousername);
                HttpConfig config = HttpConfig.custom()
                        .headers(headers)	//设置headers，不需要时则无需设置
                        .timeout(10000) 		//超时
                        .url(url)           //设置请求的url
                        .map(map)			//设置请求参数，没有则无需设置
                        .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                        //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                        //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                        //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                    .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                        //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                        //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                        //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                        ;
                String result2 = HttpClientUtil.post(config);
                JSONObject temp = JSON.parseObject(result2);
                // 获取最外层属性
                JSONObject dataObject = temp.getJSONObject("data");
                if(!dataObject.equals("")){
                    String touser_g1 = dataObject.getString("pkG1");
                    String touser_g2 = dataObject.getString("pkG2");
                    String touser_h = dataObject.getString("pkH");

                    Element pk_g1_element = pf.G1.newRandomElement();
                    int result1 = pk_g1_element.setFromBytes(pf.hexStringToBytes(touser_g1));
                    Element pk_g2_element = pf.G1.newRandomElement();
                    pk_g2_element.setFromBytes(pf.hexStringToBytes(touser_g2));
                    Element pk_h_element = pf.G1.newRandomElement();
                    pk_h_element.setFromBytes(pf.hexStringToBytes(touser_h));

                    Map<String, String> ct_map = pf.encryption(pf.GT, pf.bp, pf.Zq, pk_g1_element, pk_g2_element, pk_h_element, plainfile);
                    String secret_kf = ct_map.get("kf"); //文件秘密值
                    String CT = ct_map.get("final_ct");
                    String keywords = ct_map.get("keywords");
                    String Tw = ct_map.get("Tw");
                    keywords_textArea.setText(keywords);

                    String url2 = "http://127.0.0.1:8181/file/post_CT";
                    Map<String, Object> map2 = new HashMap<String, Object>();
                    map2.put("upload_CT", CT);
                    map2.put("upload_DO", Login_user);
                    map2.put("keywords", keywords);
                    map2.put("Tw", Tw);
                    HttpConfig config2 = HttpConfig.custom()
                            .headers(headers)	//设置headers，不需要时则无需设置
                            .timeout(10000) 		//超时
                            .url(url2)           //设置请求的url
                            .map(map2)			//设置请求参数，没有则无需设置
                            .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                            //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                            //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                            //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                    .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                            //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                            //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                            //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                            ;
                    String result3 = HttpClientUtil.post(config2);
                    JSONObject temp3 = JSON.parseObject(result3);
                    if(temp3.getIntValue("status")==200){
                        String url3 = "http://127.0.0.1:8181/local/save_kf";
                        Map<String, Object> map3 = new HashMap<String, Object>();
                        map3.put("file_id", temp3.getIntValue("data"));
                        map3.put("kf", secret_kf);
                        map3.put("owner", Login_user);
                        String jsonmap3 = JSON.toJSONString(map3);
                        HttpConfig config3 = HttpConfig.custom()
                                .headers(headers)	//设置headers，不需要时则无需设置
                                .timeout(10000) 		//超时
                                .url(url3)           //设置请求的url
                                .map(map3)			//设置请求参数，没有则无需设置
                                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                                //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                                //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                                //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                                .json(jsonmap3) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                                //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                                //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                                //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                                ;
                        String result4 = HttpClientUtil.post(config3);
                        JSONObject temp4 = JSON.parseObject(result4);
                        if(temp4.getIntValue("status")==200){
                            JOptionPane.showMessageDialog(null, "加密上传成功！");
                        }else {
                            JOptionPane.showMessageDialog(null, "上传失败！");
                        }
                    }else{
                        JOptionPane.showMessageDialog(null, "上传失败！");
                    }

                }else{
                    JOptionPane.showMessageDialog(null, "上传失败！");
                }

            }


        }
    }

    private void search_buttonActionPerformed(ActionEvent e) throws HttpProcessException {
        String testwords = search_textField.getText();
        if(testwords.equals("")){
            JOptionPane.showMessageDialog(null, "请输入关键词！");
        }else{
            String url = "http://127.0.0.1:8181/smartgrid/getuser_pk";
            Header[] headers 	= HttpHeader.custom()
                    .userAgent("javacl")
                    .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                    .other("customer", "自定义")
                    .build();
            HCB hcb= HCB.custom();
            HttpClient client = hcb.build();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("username", Login_user);
            HttpConfig config = HttpConfig.custom()
                    .headers(headers)	//设置headers，不需要时则无需设置
                    .timeout(10000) 		//超时
                    .url(url)           //设置请求的url
                    .map(map)			//设置请求参数，没有则无需设置
                    .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                    //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                    //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                    //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                    .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                    //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                    //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                    //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                    ;
            String result2 = HttpClientUtil.post(config);
            JSONObject temp = JSON.parseObject(result2);
            // 获取最外层属性
            JSONObject dataObject = temp.getJSONObject("data");
            if(temp.getIntValue("status")==200){

                String user_g1 = dataObject.getString("pkG1");

                Element pk_g1_element = pf.G1.newRandomElement();
                int result1 = pk_g1_element.setFromBytes(pf.hexStringToBytes(user_g1));

                String url3 = "http://127.0.0.1:8181/local/getuser_sk";
                Map<String, Object> map3 = new HashMap<String, Object>();
                map3.put("username", Login_user);
                HttpConfig config3 = HttpConfig.custom()
                        .headers(headers)	//设置headers，不需要时则无需设置
                        .timeout(10000) 		//超时
                        .url(url3)           //设置请求的url
                        .map(map3)			//设置请求参数，没有则无需设置
                        .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                        ;
                String result3 = HttpClientUtil.post(config3);
                JSONObject json = JSON.parseObject(result3);
                if(json.getIntValue("status")==200){
                    Element Search_sk = pf.G1.newRandomElement().setToZero();
                    Search_sk.setFromBytes(pf.hexStringToBytes(json.getString("data")));
                    String Tw = pf.trapdoor(testwords, pf.Zq, pk_g1_element, Search_sk, pf.bp);
                    if(!Tw.equals("")){
                        String url2 = "http://127.0.0.1:8181/file/Tw_search";
                        Map<String, Object> map2 = new HashMap<String, Object>();
                        map2.put("Tw", Tw);
                        HttpConfig config2 = HttpConfig.custom()
                                .headers(headers)	//设置headers，不需要时则无需设置
                                .timeout(10000) 		//超时
                                .url(url2)           //设置请求的url
                                .map(map2)			//设置请求参数，没有则无需设置
                                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                                //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                                //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                                //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                    .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                                //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                                //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                                //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                                ;
                        String result4 = HttpClientUtil.post(config2);
                        JSONObject json4 = JSON.parseObject(result4);
                        if(json4.getIntValue("status")==200){
                            List<search_param> sp = new ArrayList<search_param>();
                            JSONArray a = (JSONArray) JSONArray.parse(json4.getJSONArray("data").toString());
                            if(!a.isEmpty()){
                                for (int i=0;i<a.size();i++){
                                    sp.add(JSONObject.toJavaObject((JSON)a.get(i), search_param.class));
                                }

                                DefaultListModel defaultListModel = new DefaultListModel();
                                for (int i=0;i<sp.size();i++){
                                    defaultListModel.add(i, "id = "+sp.get(i).getid()+",DO = "+sp.get(i).getOwner());
                                }

                                id_list.setModel(defaultListModel);
                                JOptionPane.showMessageDialog(null, "查询成功！请选择您想下载并解密的文件！");
                            }else{
                                JOptionPane.showMessageDialog(null, "查询失败！数据库中没有您想要的文件！");
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "查询失败！数据库中没有您想要的文件！");
                        }
                    }else{
                        JOptionPane.showMessageDialog(null, "生成关键词陷门失败！");
                    }
                }else{
                    JOptionPane.showMessageDialog(null, "生成关键词陷门失败！");
                }

            }else{
                JOptionPane.showMessageDialog(null, "生成关键词陷门失败！");
            }

        }

    }

    private void id_listValueChanged(ListSelectionEvent e) {

    }

    private void result_buttonActionPerformed(ActionEvent e) throws HttpProcessException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
        String s = id_list.getSelectedValue().toString();
        String[] s_arr = s.split(",");
//        String sName = s_arr[1].substring(5);
        Integer sID = Integer.valueOf(s_arr[0].substring(5));;

        String url = "http://127.0.0.1:8181/file/download_id";
        Header[] headers 	= HttpHeader.custom()
                                            .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                .other("customer", "自定义")
                .build();
        HCB hcb= HCB.custom();
        HttpClient client = hcb.build();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("download_id", sID);
        HttpConfig config = HttpConfig.custom()
                .headers(headers)	//设置headers，不需要时则无需设置
                .timeout(10000) 		//超时
                .url(url)           //设置请求的url
                .map(map)			//设置请求参数，没有则无需设置
                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
              ;
        String download_CT = HttpClientUtil.post(config);
        if(!download_CT.equals("")){
            String url2 = "http://127.0.0.1:8181/local/get_kf";
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("file_id", sID);
            HttpConfig config2 = HttpConfig.custom()
                    .headers(headers)	//设置headers，不需要时则无需设置
                    .timeout(10000) 		//超时
                    .url(url2)           //设置请求的url
                    .map(map2)			//设置请求参数，没有则无需设置
                    .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                    ;
            String result = HttpClientUtil.post(config2);
            JSONObject json = JSON.parseObject(result);
            String secret_kf = json.getString("data");
            if(secret_kf.equals("")){
                JOptionPane.showMessageDialog(null, "无法获取解密密钥！");
            }else{
                String[] CT_split = download_CT.split(",");
                String CF = CT_split[CT_split.length - 1];
                CF = CF.replaceAll("[{}]", ""); // 移除 {}
                String path = SmartGrid.class.getResource("").getPath();
                String baseDir = new File(path).getParentFile().getParentFile().getParentFile().getParentFile().getPath();
                String dp = baseDir+File.separator+sID+"_plain.txt";
                String resultfile = pf.file_decryption(secret_kf, CF, dp);
            }
        }else {
            JOptionPane.showMessageDialog(null, "文件下载失败！");
        }

    }

    private void del_buttonActionPerformed(ActionEvent e) throws HttpProcessException {
        Integer delID = Integer.valueOf(del_textField.getText());

        if(delID == null){
            return;
        }

        String url = "http://127.0.0.1:8181/file/del_file";
        Header[] headers 	= HttpHeader.custom()
                                            .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                .other("customer", "自定义")
                .build();
        HCB hcb= HCB.custom();
        HttpClient client = hcb.build();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("delID", delID);
        map.put("del_DO", Login_user);
        HttpConfig config = HttpConfig.custom()
                .headers(headers)	//设置headers，不需要时则无需设置
                .timeout(10000) 		//超时
                .url(url)           //设置请求的url
                .map(map)			//设置请求参数，没有则无需设置
                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                    .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                ;
        String result2 = HttpClientUtil.post(config);
        JSONObject json2 = JSON.parseObject(result2);
        if(json2.getIntValue("status")==200){
            String url2 = "http://127.0.0.1:8181/local/del_file";
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("delID", delID);
            HttpConfig config2 = HttpConfig.custom()
                    .headers(headers)	//设置headers，不需要时则无需设置
                    .timeout(10000) 		//超时
                    .url(url2)           //设置请求的url
                    .map(map2)			//设置请求参数，没有则无需设置
                    .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                    ;
            String result = HttpClientUtil.post(config2);
            JSONObject json = JSON.parseObject(result);
            if(json.getIntValue("status")==200){

            }
            JOptionPane.showMessageDialog(null, "删除成功！");
        }else if(json2.getIntValue("status")==500){
            JOptionPane.showMessageDialog(null, "要删除的文件不存在！");
        }else{
            JOptionPane.showMessageDialog(null, "您无权删除这个文件！");
        }


    }

    private void reg_button2ActionPerformed(ActionEvent e) throws HttpProcessException {
        String reg_name = "";
        reg_name = name_textField2.getText();
        String reg_pass = "";
        reg_pass = String.valueOf(passwordField2.getPassword());

        if(!reg_name.equals("")){
            if(!reg_pass.equals("")){
                //  生成用户的公钥和私钥
                Element g1 = pf.G1.newRandomElement().getImmutable();
                Element g2 = pf.G2.newRandomElement().getImmutable();
                Map<String, Element> key_map = pf.KeyGen(pf.Zq,g1,g2);
                Element g1_e_alpha = key_map.get("sk"); //用户私钥
                Element g2_e_alpha = key_map.get("pk_h"); //h，用户公钥为{g1, g2, h}.
                String pk_g1 = pf.hexBytesToString(g1.duplicate().toBytes());
                String pk_g2 = pf.hexBytesToString(g2.duplicate().toBytes());
                String pk_h = pf.hexBytesToString(g2_e_alpha.duplicate().toBytes());
                String sk = pf.hexBytesToString(g1_e_alpha.duplicate().toBytes());

                String url = "http://127.0.0.1:8181/smartgrid/register";
                Header[] headers 	= HttpHeader.custom()
                                                    .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                        .other("customer", "自定义")
                        .build();
                HCB hcb= HCB.custom();
                HttpClient client = hcb.build();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("username", reg_name);
                map.put("password", reg_pass);
                map.put("pk_g1", pk_g1);
                map.put("pk_g2", pk_g2);
                map.put("pk_h", pk_h);
                String jsonmap = JSON.toJSONString(map);
                HttpConfig config = HttpConfig.custom()
                        .headers(headers)	//设置headers，不需要时则无需设置
                        .timeout(10000) 		//超时
                        .url(url)           //设置请求的url
                        .map(map)			//设置请求参数，没有则无需设置
                        .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                        //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                        //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                        //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                        .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                        //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                        //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                        //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                        ;
                String result = HttpClientUtil.post(config);
                JSONObject regResp = JSON.parseObject(result);

                String url2 = "http://127.0.0.1:8181/local/save_sk";
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("owner_name", reg_name);
                map2.put("sk", sk);
//                String jsonmap2 = JSON.toJSONString(map2);
                HttpConfig config2 = HttpConfig.custom()
                        .headers(headers)	//设置headers，不需要时则无需设置
                        .timeout(10000) 		//超时
                        .url(url2)           //设置请求的url
                        .map(map2)			//设置请求参数，没有则无需设置
                        .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                        //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                        //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                        //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                        .json(jsonmap2) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                        //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                        //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                        //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                        ;
                String result2 = HttpClientUtil.post(config2);
                JSONObject regResp2 = JSON.parseObject(result2);
                if(regResp.getIntValue("status") == 200 && regResp2.getIntValue("status") == 200){
                    Reg_dialog.setVisible(true);
                    String complete_pk = "{"+pk_g1+","+pk_g2+","+pk_h+"}";
                    pk_textField.setText(complete_pk);
                    sk_textField.setText(sk);
                }else{
                    JOptionPane.showMessageDialog(null, "密钥存储失败，请重新注册！");
                }
            }else{
                JOptionPane.showMessageDialog(null, "请输入密码！");
            }
        }else{
            JOptionPane.showMessageDialog(null, "请输入用户名！");
        }

    }

    private void submit_buttonActionPerformed(ActionEvent e) throws HttpProcessException {
        String sub_name = "";
        String sub_pass = "";
        sub_name = name_textField.getText();
        sub_pass = String.valueOf(passwordField1.getPassword());

        if(!sub_name.equals("")){
            if(!sub_pass.equals("")){
                if(user_radioButton.isSelected()){
                    String url = "http://127.0.0.1:8181/smartgrid/login";
                    Header[] headers 	= HttpHeader.custom()
                                                        .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                            .other("customer", "自定义")
                            .build();
                    HCB hcb= HCB.custom();
                    HttpClient client = hcb.build();
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("username", sub_name);
                    map.put("password", sub_pass);
//                String jsonmap = JSON.toJSONString(map);
                    HttpConfig config = HttpConfig.custom()
                            .headers(headers)	//设置headers，不需要时则无需设置
                            .timeout(10000) 		//超时
                            .url(url)           //设置请求的url
                            .map(map)			//设置请求参数，没有则无需设置
                            .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                            //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                            //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
//                        .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                            //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                            //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                            //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                            ;
                    String result2 = HttpClientUtil.post(config);
                    JSONObject regResp2 = JSON.parseObject(result2);
                    if(regResp2.getIntValue("status") == 200){
                        Login.setVisible(false);
                        User.setVisible(true);
                        Login_user = sub_name;
                    }else if(result2.equals("non-register")){
                        JOptionPane.showMessageDialog(null, "请注册！");
                    }else{
                        JOptionPane.showMessageDialog(null, "密码或用户名错误！");
                    }

                }else if(manager_radioButton.isSelected()){
                    String url = "http://127.0.0.1:8181/smartgrid/manager_login";
                    Header[] headers 	= HttpHeader.custom()
                            .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                            .other("customer", "自定义")
                            .build();
                    HCB hcb= HCB.custom();
                    HttpClient client = hcb.build();
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("manager_name", sub_name);
                    map.put("manager_pass", sub_pass);
                    String jsonmap = JSON.toJSONString(map);
                    HttpConfig config = HttpConfig.custom()
                            .headers(headers)	//设置headers，不需要时则无需设置
                            .timeout(10000) 		//超时
                            .url(url)           //设置请求的url
                            .map(map)			//设置请求参数，没有则无需设置
                            .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                            //.client(client)     //如果只是简单使用，无需设置，会自动获取默认的一个client对象
                            //.inenc("utf-8")   //设置请求编码，如果请求返回一直，不需要再单独设置
                            //.inenc("utf-8")   //设置返回编码，如果请求返回一直，不需要再单独设置
//                            .json(jsonmap) //json方式请求的话，就不用设置map方法，当然二者可以共用。
                            //.context(HttpCookies.custom().getContext())      //设置cookie，用于完成携带cookie的操作
                            //.out(new FileOutputStream("保存地址"))              //下载的话，设置这个方法,否则不要设置
                            //.files(new String[]{"d:/1.txt","d:/2.txt"})      //上传的话，传递文件路径，一般还需map配置，设置服务器保存路径
                            ;
                    String result2 = HttpClientUtil.post(config);
                    JSONObject regResp2 = JSON.parseObject(result2);
                    if(regResp2.getIntValue("status") == 200){
                        Login.setVisible(false);
                        Manager.setVisible(true);
                        Login_user = sub_name;

                        String url2 = "http://127.0.0.1:8181/smartgrid/get_alluser";
                        HttpConfig config2 = HttpConfig.custom()
                                .headers(headers)	//设置headers，不需要时则无需设置
                                .timeout(10000) 		//超时
                                .url(url2)           //设置请求的url
                                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                                ;
                        String result3 = HttpClientUtil.get(config2);
                        JSONObject regResp3 = JSON.parseObject(result3);
                        if(regResp3.getIntValue("status") == 200){
                            List<pemks.entity.User> sp = new ArrayList<>();
                            JSONArray a = regResp3.getJSONArray("data");
                            for (int i=0;i<a.size();i++){
                                sp.add(JSONObject.toJavaObject((JSON)a.get(i), pemks.entity.User.class));
                            }

//                            DefaultTableModel mm = new DefaultTableModel();
                            for (int i=0;i<sp.size();i++){
                                model.addRow(new Object[]{sp.get(i).getUsername(),sp.get(i).getPassword()});
                            }
//                            manage_table.setModel(mm);
                        }

                    }else if(regResp2.getIntValue("status") == 403){
                        JOptionPane.showMessageDialog(null, "您不是管理员！");
                    }else{
                        JOptionPane.showMessageDialog(null, "密码或用户名错误！");
                    }

                }

            }else{
                JOptionPane.showMessageDialog(null, "请输入密码！");
            }
        }else{
            JOptionPane.showMessageDialog(null, "请输入用户名！");
        }


    }

    private void reg_buttonActionPerformed(ActionEvent e) {
        Login.setVisible(false);
        Register.setVisible(true);

    }

    private void rs_buttonActionPerformed(ActionEvent e) {
        Reg_dialog.setVisible(false);
        Register.setVisible(false);
        Login.setVisible(true);
    }

    private void cancel_buttonActionPerformed(ActionEvent e) {
        Register.setVisible(false);
        Login.setVisible(true);
    }

    private void createUIComponents() {
        // : add custom component creation code here
    }

    private void deluser_buttonActionPerformed(ActionEvent e) throws HttpProcessException {
        int index = manage_table.getSelectedRow();
        if(index != -1){
            String del_username = (String) manage_table.getValueAt(index, 0);
//            String b = (String) manage_table.getValueAt(index, 1);
            String url = "http://127.0.0.1:8181/smartgrid/manager_del";
            Header[] headers 	= HttpHeader.custom()
                                                .userAgent("javacl")
                            .contentType("application/x-www-form-urlencoded") // 声明表单类型[1](@ref)
                    .other("customer", "自定义")
                    .build();
            HCB hcb= HCB.custom();
            HttpClient client = hcb.build();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("del_username", del_username);
            HttpConfig config = HttpConfig.custom()
                    .headers(headers)	//设置headers，不需要时则无需设置
                    .timeout(10000) 		//超时
                    .url(url)           //设置请求的url
                    .map(map)			//设置请求参数，没有则无需设置
                    .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                   ;
            String result2 = HttpClientUtil.post(config);

            if(result2.equals("success")){
                model.removeRow(index);
                JOptionPane.showMessageDialog(null, "删除成功！");
            }else{
                JOptionPane.showMessageDialog(null, "删除失败！");
            }
        }else{
            JOptionPane.showMessageDialog(null, "请选择要删除的用户！");
        }


//        model.removeRow(1);
    }

    private void back_buttonActionPerformed(ActionEvent e) {
        Manager.setVisible(false);
        Login_user = "";
        name_textField.setText("");
        name_textField2.setText("");
        passwordField1.setText("");
        passwordField2.setText("");
        search_textField.setText("");
        //---- manage_table ----
        Object[][] cellData=null;
        String[] headers = { "用户名","密码" };
        model = new DefaultTableModel(cellData, headers) {
            private static final long serialVersionUID = 1L;
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        manage_table.setModel(model);

        manage_table.setModel(model);
        id_list.setModel(new DefaultListModel());
        choose_textField.setText("");
        touser_textField.setText("");
        keywords_textArea.setText("");
        Login.setVisible(false);
        Login.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        Login = new JFrame();
        panel1 = new JPanel();
        welcome_label = new JLabel();
        name_label = new JLabel();
        pass_label = new JLabel();
        name_textField = new JTextField();
        submit_button = new JButton();
        reg_button = new JButton();
        passwordField1 = new JPasswordField();
        manager_radioButton = new JRadioButton();
        user_radioButton = new JRadioButton();
        Register = new JFrame();
        panel2 = new JPanel();
        welcome_label2 = new JLabel();
        name_label2 = new JLabel();
        pass_label2 = new JLabel();
        name_textField2 = new JTextField();
        reg_button2 = new JButton();
        cancel_button = new JButton();
        passwordField2 = new JPasswordField();
        User = new JFrame();
        tabbedPane1 = new JTabbedPane();
        upload_panel = new JPanel();
        choose_label = new JLabel();
        choose_textField = new JTextField();
        choose_button = new JButton();
        update_button = new JButton();
        keywords_label = new JLabel();
        scrollPane1 = new JScrollPane();
        keywords_textArea = new JTextArea();
        touser_label = new JLabel();
        touser_textField = new JTextField();
        search_panel = new JPanel();
        search_label = new JLabel();
        search_textField = new JTextField();
        search_button = new JButton();
        id_label = new JLabel();
        scrollPane2 = new JScrollPane();
        id_list = new JList();
        result_button = new JButton();
        del_panel = new JPanel();
        del_label = new JLabel();
        del_textField = new JTextField();
        del_button = new JButton();
        back_panel = new JPanel();
        userback_button = new JButton();
        Manager = new JFrame();
        panel3 = new JPanel();
        manage_label = new JLabel();
        scrollPane4 = new JScrollPane();
        manage_table = new JTable();
        separator1 = new JSeparator();
        panel5 = new JPanel();
        deluser_button = new JButton();
        back_button = new JButton();
        Reg_dialog = new JDialog();
        label1 = new JLabel();
        label3 = new JLabel();
        pk_textField = new JTextField();
        sk_textField = new JTextField();
        rs_button = new JButton();

        //======== Login ========
        {
            var LoginContentPane = Login.getContentPane();
            LoginContentPane.setLayout(null);

            //======== panel1 ========
            {
                panel1.setLayout(null);

                //---- welcome_label ----
                welcome_label.setText("\u6b22\u8fce\u767b\u5f55\u667a\u80fd\u7535\u7f51\u5ba2\u6237\u7aef\uff01");
                welcome_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 20));
                panel1.add(welcome_label);
                welcome_label.setBounds(70, 30, 260, welcome_label.getPreferredSize().height);

                //---- name_label ----
                name_label.setText("\u7528\u6237\u540d");
                name_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                panel1.add(name_label);
                name_label.setBounds(new Rectangle(new Point(45, 85), name_label.getPreferredSize()));

                //---- pass_label ----
                pass_label.setText("\u5bc6\u7801");
                pass_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                panel1.add(pass_label);
                pass_label.setBounds(45, 135, 48, 19);
                panel1.add(name_textField);
                name_textField.setBounds(130, 80, 205, name_textField.getPreferredSize().height);

                //---- submit_button ----
                submit_button.setText("\u767b\u5f55");
                submit_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                submit_button.addActionListener(e -> {
                    try {
                        submit_buttonActionPerformed(e);
                    } catch (HttpProcessException ex) {
                        ex.printStackTrace();
                    }
                });
                panel1.add(submit_button);
                submit_button.setBounds(80, 205, 85, 35);

                //---- reg_button ----
                reg_button.setText("\u6ce8\u518c");
                reg_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                reg_button.addActionListener(e -> reg_buttonActionPerformed(e));
                panel1.add(reg_button);
                reg_button.setBounds(225, 205, 85, 35);
                panel1.add(passwordField1);
                passwordField1.setBounds(130, 130, 205, 30);

                //---- manager_radioButton ----
                manager_radioButton.setText("\u7ba1\u7406\u5458");
                manager_radioButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
                panel1.add(manager_radioButton);
                manager_radioButton.setBounds(95, 175, 95, manager_radioButton.getPreferredSize().height);

                //---- user_radioButton ----
                user_radioButton.setText("\u666e\u901a\u7528\u6237");
                user_radioButton.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
                user_radioButton.setSelected(true);
                panel1.add(user_radioButton);
                user_radioButton.setBounds(200, 175, 105, user_radioButton.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel1.getComponentCount(); i++) {
                        Rectangle bounds = panel1.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel1.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel1.setMinimumSize(preferredSize);
                    panel1.setPreferredSize(preferredSize);
                }
            }
            LoginContentPane.add(panel1);
            panel1.setBounds(5, 5, 390, 260);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < LoginContentPane.getComponentCount(); i++) {
                    Rectangle bounds = LoginContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = LoginContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                LoginContentPane.setMinimumSize(preferredSize);
                LoginContentPane.setPreferredSize(preferredSize);
            }
            Login.pack();
            Login.setLocationRelativeTo(Login.getOwner());
        }

        //======== Register ========
        {
            var RegisterContentPane = Register.getContentPane();
            RegisterContentPane.setLayout(null);

            //======== panel2 ========
            {
                panel2.setLayout(null);

                //---- welcome_label2 ----
                welcome_label2.setText("\u6b22\u8fce\u6ce8\u518c\u667a\u80fd\u7535\u7f51\u7cfb\u7edf\uff01");
                welcome_label2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 20));
                panel2.add(welcome_label2);
                welcome_label2.setBounds(80, 40, 230, welcome_label2.getPreferredSize().height);

                //---- name_label2 ----
                name_label2.setText("\u7528\u6237\u540d");
                name_label2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                panel2.add(name_label2);
                name_label2.setBounds(new Rectangle(new Point(45, 100), name_label2.getPreferredSize()));

                //---- pass_label2 ----
                pass_label2.setText("\u5bc6\u7801");
                pass_label2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                panel2.add(pass_label2);
                pass_label2.setBounds(45, 150, 48, 19);
                panel2.add(name_textField2);
                name_textField2.setBounds(130, 95, 205, name_textField2.getPreferredSize().height);

                //---- reg_button2 ----
                reg_button2.setText("\u6ce8\u518c");
                reg_button2.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                reg_button2.addActionListener(e -> {
                    try {
                        reg_button2ActionPerformed(e);
                    } catch (HttpProcessException ex) {
                        ex.printStackTrace();
                    }
                });
                panel2.add(reg_button2);
                reg_button2.setBounds(80, 205, 85, 35);

                //---- cancel_button ----
                cancel_button.setText("\u8fd4\u56de");
                cancel_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                cancel_button.addActionListener(e -> cancel_buttonActionPerformed(e));
                panel2.add(cancel_button);
                cancel_button.setBounds(225, 205, 85, 35);
                panel2.add(passwordField2);
                passwordField2.setBounds(130, 145, 205, 30);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            RegisterContentPane.add(panel2);
            panel2.setBounds(5, 5, 390, 260);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < RegisterContentPane.getComponentCount(); i++) {
                    Rectangle bounds = RegisterContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = RegisterContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                RegisterContentPane.setMinimumSize(preferredSize);
                RegisterContentPane.setPreferredSize(preferredSize);
            }
            Register.pack();
            Register.setLocationRelativeTo(Register.getOwner());
        }

        //======== User ========
        {
            var UserContentPane = User.getContentPane();
            UserContentPane.setLayout(null);

            //======== tabbedPane1 ========
            {
                tabbedPane1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 13));

                //======== upload_panel ========
                {
                    upload_panel.setLayout(null);

                    //---- choose_label ----
                    choose_label.setText("\u8bf7\u9009\u62e9\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u8def\u5f84\uff1a");
                    choose_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    upload_panel.add(choose_label);
                    choose_label.setBounds(new Rectangle(new Point(25, 25), choose_label.getPreferredSize()));
                    upload_panel.add(choose_textField);
                    choose_textField.setBounds(20, 55, 265, 35);

                    //---- choose_button ----
                    choose_button.setText("\u9009\u62e9");
                    choose_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
                    choose_button.addActionListener(e -> choose_buttonActionPerformed(e));
                    upload_panel.add(choose_button);
                    choose_button.setBounds(305, 55, choose_button.getPreferredSize().width, 35);

                    //---- update_button ----
                    update_button.setText("\u52a0\u5bc6\u4e0a\u4f20");
                    update_button.setFont(new Font("\u65b0\u5b8b\u4f53", Font.PLAIN, 14));
                    update_button.addActionListener(e -> {
                        try {
                            update_buttonActionPerformed(e);
                        } catch (NoSuchPaddingException ex) {
                            ex.printStackTrace();
                        } catch (NoSuchAlgorithmException ex) {
                            ex.printStackTrace();
                        } catch (IllegalBlockSizeException ex) {
                            ex.printStackTrace();
                        } catch (BadPaddingException ex) {
                            ex.printStackTrace();
                        } catch (NoSuchProviderException ex) {
                            ex.printStackTrace();
                        } catch (InvalidKeyException ex) {
                            ex.printStackTrace();
                        } catch (HttpProcessException ex) {
                            ex.printStackTrace();
                        }
                    });
                    upload_panel.add(update_button);
                    update_button.setBounds(285, 105, 100, 30);

                    //---- keywords_label ----
                    keywords_label.setText("\u63d0\u53d6\u7684\u6587\u4ef6\u5173\u952e\u8bcd\u96c6\u5408\u4e3a\uff1a");
                    keywords_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    upload_panel.add(keywords_label);
                    keywords_label.setBounds(25, 155, 192, 19);

                    //======== scrollPane1 ========
                    {
                        scrollPane1.setViewportView(keywords_textArea);
                    }
                    upload_panel.add(scrollPane1);
                    scrollPane1.setBounds(25, 185, 360, 40);

                    //---- touser_label ----
                    touser_label.setText("\u6587\u4ef6\u5206\u4eab\u5bf9\u8c61");
                    touser_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    upload_panel.add(touser_label);
                    touser_label.setBounds(25, 110, 110, touser_label.getPreferredSize().height);
                    upload_panel.add(touser_textField);
                    touser_textField.setBounds(145, 105, 110, touser_textField.getPreferredSize().height);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < upload_panel.getComponentCount(); i++) {
                            Rectangle bounds = upload_panel.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = upload_panel.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        upload_panel.setMinimumSize(preferredSize);
                        upload_panel.setPreferredSize(preferredSize);
                    }
                }
                tabbedPane1.addTab("\u4e0a\u4f20", upload_panel);

                //======== search_panel ========
                {
                    search_panel.setLayout(null);

                    //---- search_label ----
                    search_label.setText("\u8bf7\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5173\u952e\u8bcd\u96c6\u5408(\u7528\u9017\u53f7\u5206\u9694)\uff1a");
                    search_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    search_panel.add(search_label);
                    search_label.setBounds(new Rectangle(new Point(25, 25), search_label.getPreferredSize()));
                    search_panel.add(search_textField);
                    search_textField.setBounds(20, 55, 265, 35);

                    //---- search_button ----
                    search_button.setText("\u67e5\u8be2");
                    search_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 15));
                    search_button.addActionListener(e -> {
                        try {
                            search_buttonActionPerformed(e);
                        } catch (HttpProcessException ex) {
                            ex.printStackTrace();
                        }
                    });
                    search_panel.add(search_button);
                    search_button.setBounds(305, 55, 90, 35);

                    //---- id_label ----
                    id_label.setText("\u67e5\u8be2\u5230\u7684\u5bc6\u6587\u6709(\u9009\u62e9\u9700\u8981\u7684\u5bc6\u6587\u8fdb\u884c\u4fdd\u5b58)\uff1a");
                    id_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    search_panel.add(id_label);
                    id_label.setBounds(new Rectangle(new Point(25, 110), id_label.getPreferredSize()));

                    //======== scrollPane2 ========
                    {

                        //---- id_list ----
                        id_list.addListSelectionListener(e -> id_listValueChanged(e));
                        scrollPane2.setViewportView(id_list);
                    }
                    search_panel.add(scrollPane2);
                    scrollPane2.setBounds(25, 140, 260, 80);

                    //---- result_button ----
                    result_button.setText("\u89e3\u5bc6\u4fdd\u5b58");
                    result_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 15));
                    result_button.addActionListener(e -> {
                        try {
                            result_buttonActionPerformed(e);
                        } catch (HttpProcessException ex) {
                            ex.printStackTrace();
                        } catch (BadPaddingException ex) {
                            ex.printStackTrace();
                        } catch (NoSuchAlgorithmException ex) {
                            ex.printStackTrace();
                        } catch (NoSuchPaddingException ex) {
                            ex.printStackTrace();
                        } catch (IllegalBlockSizeException ex) {
                            ex.printStackTrace();
                        } catch (NoSuchProviderException ex) {
                            ex.printStackTrace();
                        } catch (InvalidKeyException ex) {
                            ex.printStackTrace();
                        }
                    });
                    search_panel.add(result_button);
                    result_button.setBounds(300, 155, 100, 40);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < search_panel.getComponentCount(); i++) {
                            Rectangle bounds = search_panel.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = search_panel.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        search_panel.setMinimumSize(preferredSize);
                        search_panel.setPreferredSize(preferredSize);
                    }
                }
                tabbedPane1.addTab("\u67e5\u8be2", search_panel);

                //======== del_panel ========
                {
                    del_panel.setLayout(null);

                    //---- del_label ----
                    del_label.setText("\u8bf7\u8f93\u5165\u8981\u5220\u9664\u7684\u6587\u4ef6ID\uff1a");
                    del_label.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
                    del_panel.add(del_label);
                    del_label.setBounds(new Rectangle(new Point(25, 25), del_label.getPreferredSize()));
                    del_panel.add(del_textField);
                    del_textField.setBounds(20, 55, 265, 35);

                    //---- del_button ----
                    del_button.setText("\u786e\u5b9a");
                    del_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 15));
                    del_button.addActionListener(e -> {
                        try {
                            del_buttonActionPerformed(e);
                        } catch (HttpProcessException ex) {
                            ex.printStackTrace();
                        }
                    });
                    del_panel.add(del_button);
                    del_button.setBounds(305, 55, 90, 35);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < del_panel.getComponentCount(); i++) {
                            Rectangle bounds = del_panel.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = del_panel.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        del_panel.setMinimumSize(preferredSize);
                        del_panel.setPreferredSize(preferredSize);
                    }
                }
                tabbedPane1.addTab("\u5220\u9664", del_panel);

                //======== back_panel ========
                {
                    back_panel.setLayout(new GridBagLayout());
                    ((GridBagLayout)back_panel.getLayout()).columnWidths = new int[] {0, 105, 55, 105, 0, 0};
                    ((GridBagLayout)back_panel.getLayout()).rowHeights = new int[] {0, 0};
                    ((GridBagLayout)back_panel.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
                    ((GridBagLayout)back_panel.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};
                    //---- back_button ----
                    userback_button.setText("退出登录");
                    userback_button.addActionListener(e -> back_buttonActionPerformed(e));
                    back_panel.add(userback_button, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
                            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                            new Insets(0, 0, 0, 5), 0, 0));
                }
                tabbedPane1.addTab("退出", back_panel);
            }
            UserContentPane.add(tabbedPane1);
            tabbedPane1.setBounds(0, 0, 405, 265);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < UserContentPane.getComponentCount(); i++) {
                    Rectangle bounds = UserContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = UserContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                UserContentPane.setMinimumSize(preferredSize);
                UserContentPane.setPreferredSize(preferredSize);
            }
            User.pack();
            User.setLocationRelativeTo(User.getOwner());
        }

        //======== Manager ========
        {
            var ManagerContentPane = Manager.getContentPane();
            ManagerContentPane.setLayout(null);

            //======== panel3 ========
            {
                panel3.setLayout(new GridBagLayout());
                ((GridBagLayout)panel3.getLayout()).columnWidths = new int[] {37, 322, 32, 0};
                ((GridBagLayout)panel3.getLayout()).rowHeights = new int[] {44, 12, 143, 14, 37, 0};
                ((GridBagLayout)panel3.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 1.0E-4};
                ((GridBagLayout)panel3.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

                //---- manage_label ----
                manage_label.setText("\u7528\u6237\u7ba1\u7406");
                manage_label.setFont(manage_label.getFont().deriveFont(manage_label.getFont().getSize() + 4f));
                panel3.add(manage_label, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.VERTICAL,
                    new Insets(0, 0, 5, 5), 0, 0));

                //======== scrollPane4 ========
                {

                    //---- manage_table ----
                    manage_table.setModel(new DefaultTableModel(
                        new Object[][] {
                            {null, null},
                        },
                        new String[] {
                            "\u7528\u6237\u540d", "\u5bc6\u7801"
                        }
                    ) {
                        Class<?>[] columnTypes = new Class<?>[] {
                            String.class, String.class
                        };
                        @Override
                        public Class<?> getColumnClass(int columnIndex) {
                            return columnTypes[columnIndex];
                        }
                    });
                    Object[][] cellData=null;
                        String[] headers = { "用户名","密码" };

                        	model = new DefaultTableModel(cellData, headers) {

                                    private static final long serialVersionUID = 1L;

                                    public boolean isCellEditable(int row, int column) {
                                	    return false;
                                	  }
                                	};
                        manage_table.setModel(model);
                    scrollPane4.setViewportView(manage_table);
                }
                panel3.add(scrollPane4, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));
                panel3.add(separator1, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));

                //======== panel5 ========
                {
                    panel5.setLayout(new GridBagLayout());
                    ((GridBagLayout)panel5.getLayout()).columnWidths = new int[] {0, 105, 55, 105, 0, 0};
                    ((GridBagLayout)panel5.getLayout()).rowHeights = new int[] {0, 0};
                    ((GridBagLayout)panel5.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
                    ((GridBagLayout)panel5.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

                    //---- deluser_button ----
                    deluser_button.setText("\u5220\u9664\u7528\u6237");
                    deluser_button.addActionListener(e -> {
                        try {
                            deluser_buttonActionPerformed(e);
                        } catch (HttpProcessException ex) {
                            ex.printStackTrace();
                        }
                    });
                    panel5.add(deluser_button, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                    //---- back_button ----
                    back_button.setText("\u9000\u51fa");
                    back_button.addActionListener(e -> back_buttonActionPerformed(e));
                    panel5.add(back_button, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));
                }
                panel3.add(panel5, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));
            }
            ManagerContentPane.add(panel3);
            panel3.setBounds(5, 5, 390, 260);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < ManagerContentPane.getComponentCount(); i++) {
                    Rectangle bounds = ManagerContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = ManagerContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                ManagerContentPane.setMinimumSize(preferredSize);
                ManagerContentPane.setPreferredSize(preferredSize);
            }
            Manager.pack();
            Manager.setLocationRelativeTo(Manager.getOwner());
        }

        //======== Reg_dialog ========
        {
            var Reg_dialogContentPane = Reg_dialog.getContentPane();
            Reg_dialogContentPane.setLayout(null);

            //---- label1 ----
            label1.setText("\u6ce8\u518c\u6210\u529f\uff01\u60a8\u7684\u516c\u94a5\u4e3a");
            label1.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
            Reg_dialogContentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(85, 15), label1.getPreferredSize()));

            //---- label3 ----
            label3.setText("\u60a8\u7684\u79c1\u94a5\u4e3a");
            label3.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 16));
            Reg_dialogContentPane.add(label3);
            label3.setBounds(85, 85, 130, 19);
            Reg_dialogContentPane.add(pk_textField);
            pk_textField.setBounds(45, 40, 240, pk_textField.getPreferredSize().height);
            Reg_dialogContentPane.add(sk_textField);
            sk_textField.setBounds(45, 110, 240, sk_textField.getPreferredSize().height);

            //---- rs_button ----
            rs_button.setText("\u786e\u5b9a");
            rs_button.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 15));
            rs_button.addActionListener(e -> rs_buttonActionPerformed(e));
            Reg_dialogContentPane.add(rs_button);
            rs_button.setBounds(115, 155, 95, rs_button.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < Reg_dialogContentPane.getComponentCount(); i++) {
                    Rectangle bounds = Reg_dialogContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = Reg_dialogContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                Reg_dialogContentPane.setMinimumSize(preferredSize);
                Reg_dialogContentPane.setPreferredSize(preferredSize);
            }
            Reg_dialog.pack();
            Reg_dialog.setLocationRelativeTo(Reg_dialog.getOwner());
        }

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(manager_radioButton);
        buttonGroup1.add(user_radioButton);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    public static void main(String[] args){
        FlatDarculaLaf.install();
        SmartGrid sg = new SmartGrid();
        sg.Login.setVisible(true);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JFrame Login;
    private JPanel panel1;
    private JLabel welcome_label;
    private JLabel name_label;
    private JLabel pass_label;
    private JTextField name_textField;
    private JButton submit_button;
    private JButton reg_button;
    private JPasswordField passwordField1;
    private JRadioButton manager_radioButton;
    private JRadioButton user_radioButton;
    private JFrame Register;
    private JPanel panel2;
    private JLabel welcome_label2;
    private JLabel name_label2;
    private JLabel pass_label2;
    private JTextField name_textField2;
    private JButton reg_button2;
    private JButton cancel_button;
    private JPasswordField passwordField2;
    private JFrame User;
    private JTabbedPane tabbedPane1;
    private JPanel upload_panel;
    private JLabel choose_label;
    private JTextField choose_textField;
    private JButton choose_button;
    private JButton update_button;
    private JLabel keywords_label;
    private JScrollPane scrollPane1;
    private JTextArea keywords_textArea;
    private JLabel touser_label;
    private JTextField touser_textField;
    private JPanel search_panel;
    private JLabel search_label;
    private JTextField search_textField;
    private JButton search_button;
    private JLabel id_label;
    private JScrollPane scrollPane2;
    private JList id_list;
    private JButton result_button;
    private JPanel del_panel;
    private JLabel del_label;
    private JTextField del_textField;
    private JButton del_button;
    private JPanel back_panel;
    private JButton userback_button;
    private JFrame Manager;
    private JPanel panel3;
    private JLabel manage_label;
    private JScrollPane scrollPane4;
    private JTable manage_table;
    private JSeparator separator1;
    private JPanel panel5;
    private JButton deluser_button;
    private JButton back_button;
    private JDialog Reg_dialog;
    private JLabel label1;
    private JLabel label3;
    private JTextField pk_textField;
    private JTextField sk_textField;
    private JButton rs_button;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
